package com.nkzy.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * Create by 王建烨 on 2022/3/21 9:16
 * WebInitializer代替web.xml，是由Tomcat容器初始化
 **/

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * 获取Spring配置类，根据它创建Spring父容器
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {SpringConfig.class};
    }

    /**
     * 获取SpringMVC的配置类
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] {SpringMVCConfig.class};
    }

    /**
     * 指定前端控制器请求路径：Spring前端控制器
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 指定字符编码
     */
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("utf-8");
        return new Filter[]{filter};
    }


}
